package com.mqstudy.rabbitmq.controller;

import com.mqstudy.common.model.Message;
import com.mqstudy.common.util.MessageUtil;
import com.mqstudy.rabbitmq.producer.RabbitMQProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ控制器
 */
@Slf4j
@RestController
@RequestMapping("/rabbitmq")
public class RabbitMQController {
    
    @Autowired
    private RabbitMQProducer producer;
    
    /**
     * 发送直连消息
     */
    @PostMapping("/send/direct")
    public Map<String, Object> sendDirectMessage(@RequestBody Map<String, String> request) {
        try {
            String content = request.get("content");
            Message message = MessageUtil.createTestMessage("direct-topic", content);
            producer.sendDirectMessage(message);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "直连消息发送成功");
            result.put("messageId", message.getMessageId());
            return result;
            
        } catch (Exception e) {
            log.error("发送直连消息失败: {}", e.getMessage(), e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "发送失败: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 发送扇出消息
     */
    @PostMapping("/send/fanout")
    public Map<String, Object> sendFanoutMessage(@RequestBody Map<String, String> request) {
        try {
            String content = request.get("content");
            Message message = MessageUtil.createTestMessage("fanout-topic", content);
            producer.sendFanoutMessage(message);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "扇出消息发送成功");
            result.put("messageId", message.getMessageId());
            return result;
            
        } catch (Exception e) {
            log.error("发送扇出消息失败: {}", e.getMessage(), e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "发送失败: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 发送主题消息1
     */
    @PostMapping("/send/topic1")
    public Map<String, Object> sendTopicMessage1(@RequestBody Map<String, String> request) {
        try {
            String content = request.get("content");
            Message message = MessageUtil.createTestMessage("topic1-topic", content);
            producer.sendTopicMessage1(message);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "主题消息1发送成功");
            result.put("messageId", message.getMessageId());
            return result;
            
        } catch (Exception e) {
            log.error("发送主题消息1失败: {}", e.getMessage(), e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "发送失败: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 发送主题消息2
     */
    @PostMapping("/send/topic2")
    public Map<String, Object> sendTopicMessage2(@RequestBody Map<String, String> request) {
        try {
            String content = request.get("content");
            Message message = MessageUtil.createTestMessage("topic2-topic", content);
            producer.sendTopicMessage2(message);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "主题消息2发送成功");
            result.put("messageId", message.getMessageId());
            return result;
            
        } catch (Exception e) {
            log.error("发送主题消息2失败: {}", e.getMessage(), e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "发送失败: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 发送延迟消息
     */
    @PostMapping("/send/delay")
    public Map<String, Object> sendDelayMessage(@RequestBody Map<String, Object> request) {
        try {
            String content = (String) request.get("content");
            Integer delaySeconds = (Integer) request.get("delaySeconds");
            
            if (delaySeconds == null || delaySeconds <= 0) {
                delaySeconds = 10;
            }
            
            Message message = MessageUtil.createDelayMessage("delay-topic", content, delaySeconds);
            producer.sendDelayMessage(message, delaySeconds);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "延迟消息发送成功，延迟" + delaySeconds + "秒");
            result.put("messageId", message.getMessageId());
            result.put("delaySeconds", delaySeconds);
            return result;
            
        } catch (Exception e) {
            log.error("发送延迟消息失败: {}", e.getMessage(), e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "发送失败: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 发送优先级消息
     */
    @PostMapping("/send/priority")
    public Map<String, Object> sendPriorityMessage(@RequestBody Map<String, Object> request) {
        try {
            String content = (String) request.get("content");
            Integer priority = (Integer) request.get("priority");
            
            if (priority == null) {
                priority = 5;
            }
            
            Message message = MessageUtil.createTestMessage("priority-topic", content);
            producer.sendPriorityMessage(message, priority);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "优先级消息发送成功，优先级: " + priority);
            result.put("messageId", message.getMessageId());
            result.put("priority", priority);
            return result;
            
        } catch (Exception e) {
            log.error("发送优先级消息失败: {}", e.getMessage(), e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "发送失败: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 发送批量消息
     */
    @PostMapping("/send/batch")
    public Map<String, Object> sendBatchMessages(@RequestBody Map<String, Object> request) {
        try {
            String topic = (String) request.get("topic");
            Integer count = (Integer) request.get("count");
            
            if (topic == null) {
                topic = "batch-topic";
            }
            if (count == null || count <= 0) {
                count = 10;
            }
            
            producer.sendBatchMessages(topic, count);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "批量消息发送成功，数量: " + count);
            result.put("topic", topic);
            result.put("count", count);
            return result;
            
        } catch (Exception e) {
            log.error("发送批量消息失败: {}", e.getMessage(), e);
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "发送失败: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> result = new HashMap<>();
        result.put("status", "UP");
        result.put("service", "RabbitMQ Demo");
        result.put("port", 8081);
        return result;
    }
} 